package com.andox.fn.inf.rpc.rest.proxy;

import java.util.concurrent.atomic.AtomicBoolean;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import com.andox.fn.inf.rpc.rest.config.ServiceConfig;
import com.andox.fn.inf.rpc.rest.extension.ExtensionLoader;
import com.andox.fn.inf.rpc.rest.filter.Invoker;
import com.andox.fn.inf.rpc.rest.protocol.Protocol;
import com.andox.fn.inf.rpc.rest.util.ClassUtils;




/**   
 * @ClassName:   SpringControllerFactoryBean   
 * @author:      Andox
 * @date:        2019年7月29日 下午12:04:27   
 *     
 */
public class ServiceBean extends ServiceConfig implements InitializingBean, ApplicationContextAware{
	private static final long serialVersionUID = 4784829168853249872L;

	public ServiceBean(String interfaceClass) {
		this.setInterfaceClass(ClassUtils.forName(interfaceClass));
	}
	
	private ApplicationContext applicationContext;
	
	private Protocol protocol;
	
	private AtomicBoolean inited = new AtomicBoolean(false);
	
	public AtomicBoolean getInited() {
		return inited;
	}

	public void setInited(AtomicBoolean inited) {
		this.inited = inited;
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		this.checkSelf();
		
		//Invoker invoker = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getExtension(this.getProtocol()).createInvoker(this);
		Invoker invoker = ProxyFactory.createInvoker(this);
		this.protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getExtension(this.getProtocol());
		protocol.export(invoker);
	}
	
	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		this.applicationContext = applicationContext;
	}

	public ApplicationContext getApplicationContext() {
		return applicationContext;
	}
}
